/*
 * @Author: your name
 * @Date: 2021-03-07 18:49:00
 * @LastEditTime: 2021-03-07 18:49:14
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \relearnJS\algorithm\素数判别.js
 */
var n = 10000

function f1(n) {
    var count = 0
    for (var i = 2; i < n; i++) {
        if (isPrime(i)) count++
    }
    return count

    function isPrime(num) {
        for (var i = 2; i * i <= num; i++) {
            if (num % i === 0) return false
        }
        return true
    }
}

function f2(n) {
    var count = 0,
        notPrime = []
    for (var i = 2; i < n; i++) {
        if (!notPrime[i]) {
            count++
            for (var j = i; j < n; j += i) {
                notPrime[j] = true
            }
        }
    }
    return count
}

function f3(t) {
    var cnt = 0
    for (var i = 2; i < t; i++) {
        if (isPrime(i)) {
            cnt++
            // console.log(i)
        }
    }
    return cnt

    function isPrime(n) {
        if (n === 2 || n === 3 || n === 5) return true
        // 不与6的倍数相邻的一定不是素数
        if (n % 6 != 1 && n % 6 != 5) {
            return false
        }
        // 在6的倍数两侧也可能不是素数
        for (var i = 5; i * i <= n; i += 6) {
            if (n % i === 0 || n % (i + 2) === 0) return false
        }
        return true
    }
}



console.log(f1(n))
console.log(f2(n))
console.log(f3(n))